﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Engine.ExServer.Cache;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using GoodMan.Tool.Framework;

namespace GoodMan.Core
{
    public class OnlineManager
    {
        private static Dictionary<string, OnlineState> userOnlineState = new Dictionary<string, OnlineState>();
        
        /// <summary>
        /// 人工注销
        /// </summary>
        /// <param name="username"></param>
        public static void LogoutOnlineState(string username)
        {
            CacheBag.RemoveCache("OnlineManager:" + username);
            if (userOnlineState.Keys.Contains(username))
            {
                try
                {
                    userOnlineState.Remove(username);
                }
                catch (Exception e)
                {
                    LogHelper.Write(CommonLogger.Support, LogLevel.Error, e);
                }
            }
        }
        /// <summary>
        /// 更新在线状态
        /// </summary>
        /// <param name="username"></param>
        /// <param name="state"></param>
        public static void SetOnlineState(string username, OnlineState state)
        {
            if (!String.IsNullOrEmpty(username))
            {
                var user = new User().Find(username);
                if (user != null)
                {
                    CacheBag.Add<User>("OnlineManager:" + username,
                        user,
                        (key, value, reason) =>
                        {
                            if (reason != CacheItemRemovedReason.Removed)
                            {
                                if (reason == CacheItemRemovedReason.Scavenged)
                                {
                                    LogHelper.Write(CommonLogger.Support, LogLevel.Info, "缓存由于低优先级被移除");
                                }
                                string un = key.Replace("OnlineManager:", "");
                                if (userOnlineState.Keys.Contains(un))
                                {
                                    try
                                    {
                                        userOnlineState.Remove(un);
                                    }
                                    catch (Exception e)
                                    {
                                        LogHelper.Write(CommonLogger.Support, LogLevel.Error, e);
                                    }
                                }
                            }
                        },
                        CacheExpireStrategy.Sliding, new TimeSpan(0, 1, 0));

                    if (userOnlineState.Keys.Contains(username))
                    {
                        try
                        {
                            userOnlineState[username] = state;
                        }
                        catch(Exception e) 
                        {
                            LogHelper.Write(CommonLogger.Support, LogLevel.Error, e);
                        }
                    }
                    else
                    {
                        if (!userOnlineState.Keys.Contains(username))
                        {
                            try
                            {
                                userOnlineState.Add(username, state);
                            }
                            catch (Exception e)
                            {
                                LogHelper.Write(CommonLogger.Support, LogLevel.Error, e);
                            }
                        }
                    }
                }
            }
        }
        public static void SetOnlineState(string userName)
        {
            if (!String.IsNullOrEmpty(userName))
            {
                if (userOnlineState.Keys.Contains(userName))
                {
                    
                    try
                    {
                        OnlineState state = userOnlineState[userName];
                        SetOnlineState(userName, state);
                    }
                    catch (Exception e)
                    {
                        LogHelper.Write(CommonLogger.Support, LogLevel.Error, e);
                    }
                }
                else
                {
                    SetOnlineState(userName, OnlineState.在线);
                }
            }
        }
        public static OnlineState GetOnlineState(string userName)
        {
            if (!String.IsNullOrEmpty(userName))
            {
                if (userOnlineState.ContainsKey(userName))
                {
                    return userOnlineState[userName];
                }
            }
            return OnlineState.离线;
        }
    }

    public class UserOnlineState
    {
        public User user { get; set; }
        public OnlineState state { get; set; }
    }
    public enum OnlineState
    { 
        在线 = 1,
        暂离 = 2,
        离线 = 3
    }
}
